package org.dromara.hm.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.hm.domain.bo.HmCustomerTransferHistoryBo;
import org.dromara.hm.domain.bo.HmOrderTransferHistoryBo;
import org.dromara.hm.domain.bo.HmTodoTransferHistoryBo;
import org.dromara.hm.domain.vo.HmCustomerTransferHistoryVo;
import org.dromara.hm.domain.vo.HmOrderTransferHistoryVo;
import org.dromara.hm.domain.vo.HmTodoTransferHistoryVo;
import org.dromara.hm.service.IHmCustomerTransferHistoryService;
import org.dromara.hm.service.IHmOrderTransferHistoryService;
import org.dromara.hm.service.IHmTodoTransferHistoryService;
import org.dromara.hm.service.ITransferRevertService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author If404 Hzy
 * @Date 2025-2025/6/13-10:41
 * @Version 1.0
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/hm/transferHistory")
public class TransferHistoryController extends BaseController
{

    private final IHmCustomerTransferHistoryService customerHistoryService;
    private final IHmOrderTransferHistoryService orderHistoryService;
    private final IHmTodoTransferHistoryService todoHistoryService;
    private final ITransferRevertService transferRevertService;

    // ----- LIST QUERIES -----

    @SaCheckPermission("hm:transferHistory:list")
    @GetMapping("/customer/list")
    public TableDataInfo<HmCustomerTransferHistoryVo> listCustomerHistory(HmCustomerTransferHistoryBo bo, PageQuery pageQuery) {
        return customerHistoryService.queryPageList(bo, pageQuery);
    }

    @SaCheckPermission("hm:transferHistory:list")
    @GetMapping("/order/list")
    public TableDataInfo<HmOrderTransferHistoryVo> listOrderHistory(HmOrderTransferHistoryBo bo, PageQuery pageQuery) {
        return orderHistoryService.queryPageList(bo, pageQuery);
    }

    @SaCheckPermission("hm:transferHistory:list")
    @GetMapping("/todo/list")
    public TableDataInfo<HmTodoTransferHistoryVo> listTodoHistory(HmTodoTransferHistoryBo bo, PageQuery pageQuery) {
        return todoHistoryService.queryPageList(bo, pageQuery);
    }

    // ----- REVERT ACTIONS -----

    @SaCheckPermission("hm:transferHistory:revert")
    @Log(title = "转移记录批量撤销", businessType = BusinessType.UPDATE)
    @PostMapping("/revert/batch/{customerHistoryId}")
    public R<Void> revertBatch(@PathVariable Long customerHistoryId) {
        transferRevertService.revertBatch(customerHistoryId);
        return R.ok();
    }

    @SaCheckPermission("hm:transferHistory:revert")
    @Log(title = "客户转移记录撤销", businessType = BusinessType.UPDATE)
    @PostMapping("/revert/customer/{historyId}")
    public R<Void> revertCustomer(@PathVariable Long historyId) {
        transferRevertService.revertCustomer(historyId);
        return R.ok();
    }

    @SaCheckPermission("hm:transferHistory:revert")
    @Log(title = "订单转移记录撤销", businessType = BusinessType.UPDATE)
    @PostMapping("/revert/order/{historyId}")
    public R<Void> revertOrder(@PathVariable Long historyId) {
        transferRevertService.revertOrder(historyId);
        return R.ok();
    }

    @SaCheckPermission("hm:transferHistory:revert")
    @Log(title = "待办转移记录撤销", businessType = BusinessType.UPDATE)
    @PostMapping("/revert/todo/{historyId}")
    public R<Void> revertTodo(@PathVariable Long historyId) {
        transferRevertService.revertTodo(historyId);
        return R.ok();
    }
}
